﻿using System;
using System.Linq;
using uTILLIty.Rave.Commandline;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using uTILLIty.Rave.Contract.Job;
using uTILLIty.Rave.Core.Manager;
using System.Text;
using System.Collections.Generic;

namespace uTILLIty.Rave.Core
{


	/// <summary>
	///This is a test class for CreateCommandTest and is intended
	///to contain all CreateCommandTest Unit Tests
	///</summary>
	[TestClass]
	public class CreateCommandTest
	{
		/// <summary>
		///Gets or sets the test context which provides
		///information about and functionality for the current test run.
		///</summary>
		public TestContext TestContext { get; set; }

		#region Additional test attributes
		// 
		//You can use the following additional attributes as you write your tests:
		//
		//Use ClassInitialize to run code before running the first test in the class
		//[ClassInitialize()]
		//public static void MyClassInitialize(TestContext testContext)
		//{
		//}
		//
		//Use ClassCleanup to run code after all tests in a class have run
		//[ClassCleanup()]
		//public static void MyClassCleanup()
		//{
		//}
		//
		//Use TestInitialize to run code before running each test
		//[TestInitialize()]
		//public void MyTestInitialize()
		//{
		//}
		//
		//Use TestCleanup to run code after each test has run
		//[TestCleanup()]
		//public void MyTestCleanup()
		//{
		//}
		//
		#endregion

		/// <summary>
		///A test for ParseCommandlineMergeVariable
		///</summary>
		[TestMethod]
		public void ParseKeyValuePairEscape()
		{
			var configurationManager = new ConfigurationManager(null, null, null, null, null, null, null, new CsvManager());
			var target = new CreateCommand(configurationManager, new CsvManager(), new JobManager());
			var parseValues = new List<MergeVariable>();
			const string expression = @"server=%COMPUTERNAME%,domain=%USERDOMAIN%,formula=1\,0+2\,0=3\,0";
			target.ParseCommandlineMergeVariable(parseValues, expression, Encoding.UTF8);
			PrintKeyValuePairs(parseValues);
			Assert.AreEqual(3, parseValues.Count);
			Assert.AreEqual("1,0+2,0=3,0", parseValues.Single(i => i.Key == "formula").Value);
		}

		/// <summary>
		///A test for ParseCommandlineMergeVariable
		///</summary>
		[TestMethod]
		public void ParseKeyValuePairExpandEnv()
		{
			var configurationManager = new ConfigurationManager(null, null, null, null, null, null, null, new CsvManager());
			var target = new CreateCommand(configurationManager, new CsvManager(), new JobManager());
			var parseValues = new List<MergeVariable>();
			const string expression = @"server=%COMPUTERNAME%,domain=%USERDOMAIN%,formula=1\,0+2\,0=3\,0";
			target.ParseCommandlineMergeVariable(parseValues, expression, Encoding.UTF8);
			PrintKeyValuePairs(parseValues);
			Assert.AreEqual(3, parseValues.Count);
			Assert.AreNotEqual("%COMPUTERNAME%", parseValues.Single(i => i.Key == "server").Value);
		}

		/// <summary>
		///A test for ParseCommandlineMergeVariable
		///</summary>
		[TestMethod]
		public void ParseKeyValuePairMerge()
		{
			var configurationManager = new ConfigurationManager(null, null, null, null, null, null, null, new CsvManager());
			var target = new CreateCommand(configurationManager, new CsvManager(), new JobManager());
			var parseValues = new List<MergeVariable> { new MergeVariable("override", "no") };
			const string expression = @"override=yes,server=%COMPUTERNAME%,domain=%USERDOMAIN%,formula=1\,0+2\,0=3\,0";
			target.ParseCommandlineMergeVariable(parseValues, expression, Encoding.UTF8);
			PrintKeyValuePairs(parseValues);
			Assert.AreEqual(4, parseValues.Count);
			Assert.AreEqual("yes", parseValues.Single(i => i.Key == "override").Value);
		}

		[TestMethod]
		public void UpdateJobId()
		{
			var job = new Job { JobId = "UnitTest_UpdateJobId" };
			CreateCommand.UpdateJobId(job, 1);
			Assert.AreEqual("UnitTest_UpdateJobId-0001", job.JobId);
			CreateCommand.UpdateJobId(job, 10);
			Assert.AreEqual("UnitTest_UpdateJobId-0010", job.JobId);
		}

		private void PrintKeyValuePairs(IEnumerable<MergeVariable> parseValues)
		{
			foreach (var pair in parseValues)
			{
				Console.WriteLine("{0}={1}", pair.Key, pair.Value);
			}
		}
	}
}
